package com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.persenter;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.adapter.DevicesManagerAdapter;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.adapter.ManageProfilesPagerAdapter;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.base.BaseAdapter;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.bean.DevicesCustomConfigurationBean;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.config.RBConfig;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.inter.IonClickedItemListenter;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.contract.IManageProfilesPagerContract;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.modle.ManageProfilesPagerModle;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils.JsonUitl;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils.RecyclerViewUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/8/15.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class ManageProfilesPagerPersenter implements IManageProfilesPagerContract.IManageProfilesPagerPersenter {
    private IManageProfilesPagerContract.IManageProfilesPagerView mIManageProfilesPagerView;
    private ManageProfilesPagerAdapter manageProfilesPagerAdapter;
    private IManageProfilesPagerContract.IManageProfilesPagerModle mIManageProfilesPagerModle ;
    private Handler handler = new Handler();
    private List<DevicesCustomConfigurationBean> mlist1;
    private String TAG = ManageProfilesPagerPersenter.class.getSimpleName();
    private RecyclerViewUtils mRecyclerViewUtils;
    private RecyclerView tRecyclerView;
    private String file_device_attrib ;
    public ManageProfilesPagerPersenter(IManageProfilesPagerContract.IManageProfilesPagerView mIManageProfilesPagerView) {
        this.mIManageProfilesPagerView = mIManageProfilesPagerView;
        mIManageProfilesPagerModle = new ManageProfilesPagerModle();
    }

    @Override
    public void initTtitle() {
        mIManageProfilesPagerView.showTitle();
    }

    @Override
    public void initRecyclerView(RecyclerView mRecyclerView, Context mContext) {
        mRecyclerViewUtils = new RecyclerViewUtils();
        manageProfilesPagerAdapter = new ManageProfilesPagerAdapter<DevicesCustomConfigurationBean>(mContext);
        mRecyclerViewUtils.initView(mRecyclerView,0,mContext,manageProfilesPagerAdapter);
        setDevicesManagerAdapterListener(manageProfilesPagerAdapter,mContext);
    }

    @Override
    public void loadDate(final Context mContext, String id) {
        file_device_attrib = id;
        OkHttpUtils
                .post()
                .url(RBConfig.HOST_IP+RBConfig.GET_DEVICES_ATTRIB)
                .addHeader("sessionId",mIManageProfilesPagerModle.getToken(mContext))
                .addParams("deviceId", id)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        // Log.e(TAG, "loadRecyclerViewDate:token ==sdg111===> "+ response.body().string());
                        String NetWorkResponse = JsonUitl.NetWorkResponse(response);
                        if(NetWorkResponse != null){
                            JSONObject object = JsonUitl.NetworkResponseDateDeal(NetWorkResponse);
                            if(object != null){
                                int codo = JsonUitl.getParsinCode(object);
                                if(codo == 100){
                                    String normalDate = JsonUitl.getStringDateFeld(object);
                                    Type type = new TypeToken<List<DevicesCustomConfigurationBean>>(){}.getType();
                                    mlist1 = new Gson().fromJson(normalDate,type);
                                    handler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            manageProfilesPagerAdapter.AddItemDate(mlist1);
                                            if(mlist1.size() < 5){
                                                mIManageProfilesPagerView.showCVConfig();
                                            }else if(mlist1.size()==0){
                                                mIManageProfilesPagerView.hidenCVconfig();
                                            }else{
                                                mIManageProfilesPagerView.hidenCVconfig();
                                            }
                                        }
                                    });
                                }
                            }
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    @Override
    public void onClickedAddConfig() {
        if(mlist1 != null && mlist1.size()>0){
           String deviceesid =  mlist1.get(0).getDeviceId();
            if(deviceesid != null && !TextUtils.isEmpty(deviceesid)){
                mIManageProfilesPagerView.toAddConfigPager(deviceesid);
            }else{
                mIManageProfilesPagerView.hidenCVconfig();
            }
        }else{
            mIManageProfilesPagerView.hidenCVconfig();
        }

    }


    /**
     * @desc 设置监听
     * */
    private void setDevicesManagerAdapterListener(BaseAdapter managerAdapter, final Context mContext) {
        managerAdapter.setmIonClickedItemListenter(new IonClickedItemListenter() {
            @Override
            public void onClickedItem(View view) {

            }

            @Override
            public void onClickedFist(int postion) {
                //  todo 删除
                if(mlist1 != null && mlist1.size() >0){
                    DevicesCustomConfigurationBean bean =    mlist1.get(postion);
                    if(bean != null ){
                      String devicesId =   bean.getDeviceId();
                     String id =  bean.getId();
                     if(id != null&& !TextUtils.isEmpty(id)){
                         if(devicesId != null && !TextUtils.isEmpty(devicesId)){
                             delectConfigFile(mContext,devicesId,id);
                         }
                     }
                    }
                }
            }
            @Override
            public void onClickedSecond(int postion) {
                //  todo 设置为当前有效配置
                if(mlist1 != null && mlist1.size() >0){
                    DevicesCustomConfigurationBean bean =    mlist1.get(postion);
                    if(bean != null ){
                        String devicesId =   bean.getDeviceId();
                        String id =  bean.getId();
                        if(id != null&& !TextUtils.isEmpty(id)){
                            if(devicesId != null && !TextUtils.isEmpty(devicesId)){
                               setDefaultCopnfigFile(mContext,devicesId,id);
                            }
                        }
                    }
                }
            }
        });
    }

    /**
     * @desc 删除配置文件
     * */
    private void delectConfigFile(final Context mContext, String devicesId, final String Myid) {
        OkHttpUtils
                .post()
                .url(RBConfig.HOST_IP+RBConfig.DELECT_CONFIG_FILE)
                .addHeader("sessionId",mIManageProfilesPagerModle.getToken(mContext))
                .addParams("deviceId", devicesId)
                .addParams("id", Myid)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                         //Log.e(TAG, "loadRecyclerViewDate:token ==sdg111===> "+ response.body().string());
                        String NetWorkResponse = JsonUitl.NetWorkResponse(response);
                        if(NetWorkResponse != null){
                            JSONObject object = JsonUitl.NetworkResponseDateDeal(NetWorkResponse);
                            if(object != null){
                                int codo = JsonUitl.getParsinCode(object);
                                final String normalDate = JsonUitl.getStringDateFeld(object);
                                if(codo == 100){
                                    // todo 刷新数据
                                    mlist1 = null;
                                    loadDate( mContext, file_device_attrib);
                                }
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        mIManageProfilesPagerView.showToastMessage(normalDate);
                                    }
                                });
                            }
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });

    }

    /**
     * @desc 设置为当前有效配置
     * */
    private void setDefaultCopnfigFile(final Context mContext, String devicesId, final String Myid) {
        OkHttpUtils
                .post()
                .url(RBConfig.HOST_IP+RBConfig.SET_CONFIG_DEFUALT_FILE)
                .addHeader("sessionId",mIManageProfilesPagerModle.getToken(mContext))
                .addParams("deviceId", devicesId)
                .addParams("id", Myid)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        //Log.e(TAG, "loadRecyclerViewDate:token ==sdg111===> "+ response.body().string());
                        String NetWorkResponse = JsonUitl.NetWorkResponse(response);
                        if(NetWorkResponse != null){
                            JSONObject object = JsonUitl.NetworkResponseDateDeal(NetWorkResponse);
                            if(object != null){
                                int codo = JsonUitl.getParsinCode(object);
                                final String normalDate = JsonUitl.getStringDateFeld(object);
                                if(codo == 100){
                                    // todo 刷新数据
                                    mlist1 = null;
                                    loadDate( mContext, file_device_attrib);
                                }
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        mIManageProfilesPagerView.showToastMessage(normalDate);
                                    }
                                });
                            }
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

}
